.TH std::getenv 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::getenv \- std::getenv

.SH Synopsis
   Defined in header <cstdlib>
   char* getenv( const char* env_var );

   Searches the environment list provided by the host environment (the OS), for a
   string that matches the C string pointed to by env_var and returns a pointer to the
   C string that is associated with the matched environment list member.

   This function is not required to be thread-safe. Another call to
   getenv, as well as a call to the POSIX functions setenv(), unsetenv(), \fI(until C++11)\fP
   and putenv() may invalidate the pointer returned by a previous call or
   modify the string obtained from a previous call.
   This function is thread-safe (calling it from multiple threads does
   not introduce a data race) as long as no other function modifies the
   host environment. In particular, the POSIX functions setenv(),         \fI(since C++11)\fP
   unsetenv(), and putenv() would introduce a data race if called without
   synchronization.

   Modifying the string returned by getenv invokes undefined behavior.

.SH Parameters

   env_var - null-terminated character string identifying the name of the environmental
             variable to look for

.SH Return value

   Character string identifying the value of the environmental variable or null pointer
   if such variable is not found.

.SH Notes

   On POSIX systems, the environment variables are also accessible through the global
   variable environ, declared as extern char** environ; in <unistd.h>, and through the
   optional third argument, envp, of the main function.

.SH Example


// Run this code

 #include <cstdlib>
 #include <iostream>

 int main()
 {
     if (const char* env_p = std::getenv("PATH"))
         std::cout << "Your PATH is: " << env_p << '\\n';
 }

.SH Possible output:

 Your PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

.SH See also

   C documentation for
   getenv
